﻿package app.demo.MyTouchApp{
 
    import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.net.navigateToURL; 
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import flash.desktop.*;
 
    public class selectAvatar2 extends MovieClip {
 
        private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		private var images:Array = ["darkBackground.png","shapesinahole.png","character.png"];
		var image3:Loader = new Loader();
		var movie2:MovieClip = new MovieClip();
		var square:Sprite = new Sprite();
		var enable:Boolean;
		
		
		//////////////////(NEW STUFF I ADDED)
		var topicLabel:Label = new Label();
		var imageBack:Loader = new Loader();
		var chooseLabel:Label = new Label();
		public var buttonBack:Button;
		public var buttonExit:Button;
		public var buttonSkip:Button;
		public var buttonConfirm:Button;
		//private var avatars:Array = ["av1.jpg", "av2.jpg", "av3.jpg", "av4.jpg","av5.jpg","av6.jpg","av7.jpg","av8.jpg","av9.jpg","av10.jpg"];
		private var avatars:Array = ["av11.png", "av22.png", "av33.png", "av44.png","av55.png","av66.png","av77.png","av88.png","av99.png","av100.png"];
		
		var av1:Loader = new Loader();
		var av2:Loader = new Loader();
		var av3:Loader = new Loader();
		var av4:Loader = new Loader();
		var av5:Loader = new Loader();
		var av6:Loader = new Loader();
		var av7:Loader = new Loader();
		var av8:Loader = new Loader();
		var av9:Loader = new Loader();
		var av10:Loader = new Loader();
		////////////////////////////////////
		
		//public var newButton:Button;
		
 		var index2:int;
		var myMaster:master;
		var topic:Loader = new Loader();
		var choose:Loader = new Loader();
		
        public function selectAvatar2(m:master):void {
			
			//fscommand("allowscale", "false");
			//fscommand("fullscreen", "false");
			//Stage.showMenu = false;
			
 			enable=false;
 			myMaster=m;
            //--------connect to TUIO-----------------
            TUIO.init(this,'localhost',3000,'',true);
            trace("MyTouchApp Initialized");
            //---------------------------------------- 
 			
			//Adding the first movie
			//addChild(movie1);
			addChild(movie2);
			
            //addEventListener(TouchEvent.MOUSE_DOWN, downEvent, false, 0, true); // run when finger down
            addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
            addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running

		/////////////////////////////////////////////////////////
		//////////////////////AVATARS PAGE///////////////////////
		//Adding the stuff 3ala movie2:
		
		//Background Page:
		imageBack.load(new URLRequest(images[0]));
		//imageBack.x=150;
		//imageBack.y=160;
		imageBack.scaleX=1.4;
		//imageBack.scaleY=0.8;
		movie2.addChild(imageBack);
		
		//Page Title (TOPIC MATCHING):
		var myTextFormat1:TextFormat = new TextFormat();
		myTextFormat1.font = "Kristen ITC";
		myTextFormat1.size = 70;
		
		topicLabel.text = "Topic Matching";
		topicLabel.width= 600;
		topicLabel.height= 300;
		topicLabel.x=260;
		topicLabel.visible=true;
		topicLabel.setStyle("textFormat", myTextFormat1);
		//movie2.addChild(topicLabel);

		topic.load(new URLRequest(images[1]));
		topic.x=240;
		topic.scaleX=1.1;
		topic.scaleY=1.1;
		movie2.addChild(topic);


		//Label (Choose Your character):
		var myTextFormat2:TextFormat = new TextFormat();
		myTextFormat2.font = "Kristen ITC";
		myTextFormat2.size = 30;
		
		chooseLabel.text = "Choose your Character:";
		chooseLabel.width= 600;
		chooseLabel.height= 300;
		chooseLabel.x=60;
		chooseLabel.y=130;
		chooseLabel.visible=true;
		chooseLabel.setStyle("textFormat", myTextFormat2);
		//movie2.addChild(chooseLabel);
		
		choose.load(new URLRequest(images[2]));
		choose.x=20;
		choose.y=80;
		//choose.scaleX=1.1;
		//choose.scaleY=1.1;
		movie2.addChild(choose);
		
		//Back button: (Drag and Drop Button)
		buttonBack = new Button();
		//buttonBack.x=-450;
		//buttonBack.y=-320;
		buttonBack.label="BACK";
		buttonBack.width=100;
		buttonBack.height=40;
		//buttonBack.emphasized=true;
		buttonBack.addEventListener(TouchEvent.MOUSE_DOWN,backMethod);
		movie2.addChild(buttonBack);
		
		//newButton= new Button();
//		newButton.x=150;
//		movie2.addChild(newButton);
		//myButton.setStyle("textFormat", tf);
		
		//Skip button:
		buttonSkip = new Button;
		buttonSkip.x=860;
		buttonSkip.y=585;
		buttonSkip.width=100;
		buttonSkip.height=40;
		buttonSkip.width=140;
		buttonSkip.label="SKIP TO GAME";
		movie2.addChild(buttonSkip);
		buttonSkip.addEventListener(TouchEvent.MOUSE_DOWN,skipAll);
		
		//Exit button:
		buttonExit = new Button;
		buttonExit.x=860;
		buttonExit.y=650;
		buttonExit.width=100;
		buttonExit.height=40;
		buttonExit.width=140;
		buttonExit.label="EXIT";
		buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
		movie2.addChild(buttonExit);
		
		
		//Avatars Images:
		
		av1.load(new URLRequest(avatars[0]));
		av1.x=160;
		av1.y=200;
		av1.scaleX=1.1;
		av1.scaleY=1.1;
		av1.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod1);
		movie2.addChild(av1);
		
		av2.load(new URLRequest(avatars[1]));
		av2.x=300;
		av2.y=200;
		av2.scaleX=1.1;
		av2.scaleY=1.1;
		av2.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod2);
		movie2.addChild(av2);
		
		av3.load(new URLRequest(avatars[2]));
		av3.x=450;
		av3.y=200;
		av3.scaleX=1.1;
		av3.scaleY=1.15;
		av3.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod3);
		movie2.addChild(av3);
		
		av4.load(new URLRequest(avatars[3]));
		av4.x=590;
		av4.y=200;
		av4.scaleX=1.1;
		av4.scaleY=1.1;
		av4.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod4);
		movie2.addChild(av4);
		
		av5.load(new URLRequest(avatars[4]));
		av5.x=740;
		av5.y=200;
		av5.scaleX=1.1;
		av5.scaleY=1.1;
		av5.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod5);
		movie2.addChild(av5);
		
		av6.load(new URLRequest(avatars[5]));
		av6.x=160;
		av6.y=350;
		av6.scaleX=1.1;
		av6.scaleY=1.1;
		av6.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod6);
		movie2.addChild(av6);
		
		av7.load(new URLRequest(avatars[6]));
		av7.x=300;
		av7.y=350;
		av7.scaleX=1.1;
		av7.scaleY=1.1;
		av7.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod7);
		movie2.addChild(av7);
		
		av8.load(new URLRequest(avatars[7]));
		av8.x=450;
		av8.y=350;
		av8.scaleX=1.1;
		av8.scaleY=1.15;
		av8.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod8);
		movie2.addChild(av8);
		
		av9.load(new URLRequest(avatars[8]));
		av9.x=590;
		av9.y=350;
		av9.scaleX=1.15;
		av9.scaleY=1.1;
		av9.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod9);
		movie2.addChild(av9);
		
		av10.load(new URLRequest(avatars[9]));
		av10.x=740;
		av10.y=350;
		av10.scaleX=1.1;
		av10.scaleY=1.1;
		av10.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod10);
		movie2.addChild(av10);
		
		//Confirm Button:
		buttonConfirm = new Button;
		buttonConfirm.x=860;
		buttonConfirm.y=520;
		buttonConfirm.width=140;
		buttonConfirm.height=40;
		buttonConfirm.label="CONFIRM";
		buttonConfirm.enabled=false;
		//buttonConfirm.touchEnabled=false;
		buttonConfirm.addEventListener(TouchEvent.MOUSE_DOWN,goTo);
		movie2.addChild(buttonConfirm);

        }
		
		////////////////////////////////////////////////
		//Methods for dimming the avatar when selected:
		private function dimMethod1(e:TouchEvent):void
		{
			index2 = 0;
			av1.alpha=0.5;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod2(e:TouchEvent):void
		{
			index2 = 1;
			av1.alpha=1.0;
			av2.alpha=0.5;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod3(e:TouchEvent):void
		{
			index2 = 2;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=0.5;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod4(e:TouchEvent):void
		{
			index2 = 3;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=0.5;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod5(e:TouchEvent):void
		{
			index2 = 4;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=0.5;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod6(e:TouchEvent):void
		{
			index2 = 5;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=0.5;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod7(e:TouchEvent):void
		{
			index2 = 6;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=0.5;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod8(e:TouchEvent):void
		{
			index2 = 7;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=0.5;
			av9.alpha=1.0;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		private function dimMethod9(e:TouchEvent):void
		{
			index2 = 8;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=0.5;
			av10.alpha=1.0;
			buttonConfirm.enabled=true;
			enable=true;
		}
		
		private function dimMethod10(e:TouchEvent):void
		{
			index2 = 9;
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=0.5;
			buttonConfirm.enabled=true;
			enable=true;
		}
		////////////////////////////////////////////////
		
		
		////////////////////////////////////////////////////////////////
		//Back Method (When back is clicked return to the previous page)
		private function backMethod(e:TouchEvent):void
		{
			//Just returning back to the main page...CHANGE IT LATER!
			av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			//removeChild(movie2);
			//addChild(movie1);
			buttonConfirm.enabled=false;
			enable=false;
			myMaster.backToPlayers();
		}
		
		////////////////////////////////////////////////////////////////
		function closeApp(event:TouchEvent):void
		{
     		 fscommand("quit");    
     	}
		
		private function skipAll(event:TouchEvent):void
		{
			myMaster.goToGameBoard();
			
		}
		
		
		
		private function goTo(e:TouchEvent):void
		{
			/*av1.alpha=1.0;
			av2.alpha=1.0;
			av3.alpha=1.0;
			av4.alpha=1.0;
			av5.alpha=1.0;
			av6.alpha=1.0;
			av7.alpha=1.0;
			av8.alpha=1.0;
			av9.alpha=1.0;
			av10.alpha=1.0;
			*/
			//buttonConfirm.enabled=false;
			if(enable==true)
			myMaster.goToKeyboard();
		}
		
		/*private function drawButtonState(rgb:uint):Sprite {
		var sprite:Sprite = new Sprite();
		//sprite.graphics.lineStyle(4,0x33621E,1);
		sprite.graphics.beginFill(rgb);
		sprite.graphics.drawRoundRect(((this.stage.
		stageWidth/2)-(numButtonWidth/2)),((this.
		stage.stageHeight/2)-(numButtonHeight/2)),
		numButtonWidth,numButtonHeight,10,10);
		return sprite;
		}*/
 
        // updates/draws the drawn lines by:
        // cycling through all existing blobs
        // getting the id of each blob
        // use that id to get the line, as the lines are named after their blobs (eg line1280 )
        // tuioobj is the blob - see how it is called by request using the blob id?
        // if it is not there remove it from the blobs array.
        // draw the next segment of the line using lineTo and current loc (tuioobj.x, tuioobj.y)
        function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
        }
		/*function start():void{
			var aButton:SimpleButton=new SimpleButton;
			

			aButton.addEventListener(MouseEvent.CLICK, clickHandler);
			//aButton.label = "Submit";
			addChild(aButton);
		}*/
 
        // when touched, this calls addblob with the blob ID and the X/Y of where it started
        public function downEvent(e:TouchEvent):void {
            var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
            addBlob(e.ID, curPt.x, curPt.y);
            e.stopPropagation();
        }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
            removeBlob(e.ID);
            e.stopPropagation();
        }
 
        // This takes the sent ID, x,y and puts it all in the blobs array
        // It then creats a new sprite, and sets the starting loc of the line
        // The line is put in an array called lines, so it can be called over and over by name
        // if the lines did not have unique names you could only draw one line at a time
        // so each line is named in the array after the id of the blob (EG line1280)
        function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
        // when called, this function removes the blobs from the array
        // cycles through the blobs array until the blob is found by its ID, then it is removed.
        function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
        }
 
    }
}